/**
 * 
 */
package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 黑龙江省
 * 排名:完成
 * 去年排名:完成
 * 
 * @author chenmin<chenmin@mouge.wang>
 * @since 2015-06-24
 */
public class Heilongjiang extends Exam implements IRank
{

	public Heilongjiang(Student student)
	{
		super(student);
	}

	public static void main(String[] args)
	{
		Student student = new Student();
		student.setStudentId("于盛伟");
		student.setStudentPwd("0623050009");
		student.setIdCard("230622199705070856");
		student.setType("w");
		Heilongjiang guangxi = new Heilongjiang(student);
		System.out.println(guangxi.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(guangxi.login(captcha));
	}

	@Override
	public String getCaptcha()
	{
		Request captchaRequest = new Request("http://221.207.246.181:8001/JWWebGaokao/Kaptcha.jpg", mSha1);

		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://221.207.246.181:8001/JWWebGaokao/studentloginCheckScore", mSha1);
		loginRequest.addHeader("Referer", "http://221.207.246.181:8001/JWWebGaokao/toStudentLoginQuery");
		loginRequest.addParams("xm", mStudent.getStudentId());
		loginRequest.addParams("ksh", mStudent.getStudentPwd());
		loginRequest.addParams("sfzh", mStudent.getIdCard());
		loginRequest.addParams("authCode", captcha);
		String loginHtml = curlPost(loginRequest);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("总分"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("验证码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else if (loginHtml.contains("请重新核实您的信息"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document resultDoc = Jsoup.parse(parseHtml);
		Elements tables = resultDoc.select("table");
		String sum = regexMatch(tables.get(2).select("tr").get(2).select("td").get(6).html(), "(.*)&nbsp");
		String regexSum = regexMatch(sum, "(.*)\\.");

		JSONObject json = new JSONObject();
		json.put("exam_id", regexMatch(tables.get(2).select("tr").get(2).select("td").get(0).html(), "(.*)&nbsp"));
		json.put("name", regexMatch(tables.get(2).select("tr").get(2).select("td").get(1).html(), "(.*)&nbsp"));
		json.put("ranking", getRank(12, regexSum, mStudent.getType()));
		json.put("province", "黑龙江省");

		JSONObject score = new JSONObject();
		score.put("语文", regexMatch(tables.get(2).select("tr").get(2).select("td").get(2).html(), "(.*)&nbsp"));
		score.put("数学", regexMatch(tables.get(2).select("tr").get(2).select("td").get(3).html(), "(.*)&nbsp"));
		score.put("外语", regexMatch(tables.get(2).select("tr").get(2).select("td").get(4).html(), "(.*)&nbsp"));
		score.put("综合", regexMatch(tables.get(2).select("tr").get(2).select("td").get(5).html(), "(.*)&nbsp"));
		score.put("总分", sum);
		score.put("备注", regexMatch(tables.get(2).select("tr").get(2).select("td").get(7).html(), "(.*)&nbsp"));
		json.put("score", score.toString());

		return json.toString();
	}

	@Override
	public void initRank()
	{
		// 理 http://www.lzk.hl.cn/gkpd/gkxx/201506/W020150624376862604928.htm
		// 文 http://www.lzk.hl.cn/gkpd/gkxx/201506/W020150624376862717235.htm
		Request request = new Request("http://www.lzk.hl.cn/gkpd/gkxx/201506/W020150624376862604928.htm");
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 3; i < trs.size() - 1; i++)
		{
			Integer score = Integer.valueOf(trs.get(i).select("td").get(0).text());
			Integer counter = Integer.valueOf(trs.get(i).select("td").get(1).text());
			Integer ranking = Integer.valueOf(trs.get(i).select("td").get(2).text());

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(12 + 31);
			GaoKaoRankService rankService = new GaoKaoRankService();
			System.out.println(score);
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(score);
			rank.setCount(counter);
			rank.setType("l");
			rank.setRanking(ranking);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}

	}

	@Override
	public void lastYearRank()
	{
		// 理 http://www.gxeduw.com/gaokao/2014/107411.html | http://www.gxeduw.com/gaokao/2014/107411_2.html | http://www.gxeduw.com/gaokao/2014/107411_3.html
		// 文 http://www.gxeduw.com/gaokao/2014/107412.html | http://www.gxeduw.com/gaokao/2014/107412_2.html | http://www.gxeduw.com/gaokao/2014/107412_3.html
		Request request = new Request("http://www.gxeduw.com/gaokao/2014/107411_3.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));
		
		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text());
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());
			System.out.println(scores + ":" + counters + ":" + rankings);
			
			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(12);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}

	}
}
